package com.qire.manhua.activity;

import android.os.Bundle;

import com.qire.antsbinder.dal.exception.DataAccessException;
import com.qire.antscore.annotation.DalExceptionObserve;
import com.qire.antscore.annotation.RouteMapping;
import com.qire.antsrouter.AntsRouter;
import com.qire.antsrouter.card.FloorCard;
import com.qire.antsrouter.inject.Autowired;
import com.qire.common.constant.Navigation;
import com.qire.common.constant.Url;
import com.qire.common.dal.exception.NetApiException;
import com.qire.common.inject.EventControllerInjector;
import com.qire.common.inject.ViewModelInjector;
import com.qire.common.support.DialogFastBuildUtil;
import com.qire.common.support.DialogFastBuildUtil.Builder;
import com.qire.common.support.base.BaseActivity;
import com.qire.common.support.base.ProxyViewModel;
import com.qire.common.utils.ThemeUtil;
import com.qire.common.utils.ToastHelper;
import com.qire.common.widget.snackbar.Prompt;
import com.qire.manhua.R;
import com.qire.manhua.controller.AccountsAndSecurityController;
import com.qire.manhua.databinding.ActivityAccountsAndSecurityBinding;
import com.qire.manhua.dialog.TipsConfirmDialog;
import com.qire.manhua.model.viewModel.IAccountsAndSecurityViewModel;

import androidx.annotation.Nullable;

@RouteMapping(path = Navigation.Page.AccountsAndSecurityPage)
public class AccountsAndSecurityActivity extends BaseActivity<ActivityAccountsAndSecurityBinding> {

    @Autowired(injector = ViewModelInjector.class)
    ProxyViewModel<IAccountsAndSecurityViewModel> viewModel;

    @Autowired(injector = EventControllerInjector.class)
    AccountsAndSecurityController controller;

    @Override
    public int layoutRid() {
        return R.layout.activity_accounts_and_security;
    }

    @Override
    public void finish() {
        int pageNo = viewModel.getToValue("pageNo", 0);
        if(pageNo >= 1) {
            viewModel.proxy().gotoTabPage(0);
            return;
        }
        super.finish();
    }

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        ThemeUtil.setStatusBarColor(this, R.color.white);
        ThemeUtil.changeStatusBarLightOrDark(this, true, true);

        controller.onAddition(viewModel);
    }

    @DalExceptionObserve(tags = {Url.WeChatBind, Url.QqBind})
    public void thirdPlatformBindExceptionHandle(DataAccessException e) {
        if(e instanceof NetApiException) {
            final Builder builder = DialogFastBuildUtil.builder(R.layout.dialog_common_prompt_light_r);
            builder.setTextView(R.id.dialog_title, "绑定失败")
                    .setTextView(R.id.dialog_content, "该第三方平台账号已注册，无法完成绑定")
                    .setTextView(R.id.dialog_button_dark, "取消", v -> {
                        builder.dismiss();
                        v.setOnClickListener(null);
                    })
                    .setTextView(R.id.dialog_button_light, "确认解绑", v -> {
                        AntsRouter.ROUTER.<FloorCard>buildCard(Navigation.Page.WebPage)
                                .withData("title", "解决方案")
                                .withData("url", Url.thirdPlatformCanNotBindSolutionURL)
                                .navigation();
                        v.setOnClickListener(null);
                    })
                    .show();
        }
        ToastHelper.showSnackBar(e.extraMsg, Prompt.ERROR);
    }

    @DalExceptionObserve(tags = {Url.WeChatUnbind, Url.QqUnbind})
    public void thirdPlatformUnbindExceptionHandle(DataAccessException e) {
        if(e instanceof NetApiException) {
            switch (((NetApiException)e).errorCode) {
                case 100071:
                    new TipsConfirmDialog(this).setDialogTitle("")
                            .setDialogContent("当前绑定的第三方平台账号与授权\n账号不能匹配，无法完成解绑")
                            .setDialogBtnText("好的")
                            .show();
                    return;
                case 100072:
                    ToastHelper.showCenterDarkToast("",e.extraMsg);
                    return;
            }
        }
        ToastHelper.showSnackBar(e.extraMsg, Prompt.ERROR);
    }

}
